/**
 *<p>@Package: net.dgg.zz.flownode.controller</p>
 *<p>@Title：package-info</p>
 *<p>@description:</p>
 *<p>@author liulei</p>
 *<p>@date 2018年8月27日</p>
 *<p>@Company dgg</p>
 */
package net.dgg.zz.flownode.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.flownode.service.FlowNodeService;
import net.dgg.zz.production.inProcess.entity.ProductNode;
import net.dgg.zz.production.inProcess.entity.ProductNodeField;
import net.dgg.zz.production.inProcess.entity.ProductNodeFlow;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/***
 * 流程节点配置controller
 *<p>@Package: net.dgg.zz.flownode.controller</p>
 *<p>@Title：FlowNodeController</p>
 *<p>@description:</p>
 *<p>@author liulei</p>
 *<p>@date 2018年8月27日</p>
 *<p>@Company dgg</p>
 */
@Controller
@RequestMapping("/zzsc/flownode")
public class FlowNodeController extends DggBaseController {
	
	
    @Autowired
    private FlowNodeService flowNodeService;
    @Autowired
    private TranslateUtil translateUtil;
	
    /**
     * 节点管理列表页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public String Index() {
        return "zzsc/nodeManager/flow_manager_list";
    }

    /**
     * @Title:
     * @Description: TODO 查询出全部节点流程
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/4/27
     **/
    @RequestMapping(value = "search")
    @ResponseBody
    public DataTableResponse SearchAllNodeFlow(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        params.put("start", (r.getPage() - 1) * r.getLength());
        params.put("limit", r.getLength());
        Map fileMap = new HashMap();
        fileMap.put("updateTime", "updateTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        String[] colNames = new String[]{"bigformatCode", "businessCode"};
        List<ProductNodeFlow> list = flowNodeService.findAllFlowWithPage(params);
        list = translateUtil.translateList(colNames, list);
        Integer count = flowNodeService.countAllFlowWithPage(params);
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }

    /**
     * 节点管理——新建流程页
     *
     * @return
     */
    @RequestMapping(value = "/flowcreate_tabpage")
    public String FlowCreate() {
        return "zzsc/nodeManager/flowcreate_tabpage";
    }

    /**
     * @Title:
     * @Description: TODO
     * @param: 节点管理——新建流程保存
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/4/28
     **/
    @NLog
    @AuthOpt(code = "ZZSC_NODEMANAGER_CREATEFLOW")
    @RequestMapping(value = "/createNewNodeFlowSave")
    @ResponseBody
    public DggRestResponse createNewNodeFlowSave(@RequestParam Map params) {
        try {
            flowNodeService.saveFlow(params);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }

    /**
     * 节点管理——流程编辑页
     *
     * @return
     */
    @RequestMapping(value = "/flowedit_tabpage")
    public String FlowEdit(@RequestParam Map params, Model model) {
        params.put("flag", "1");
        ProductNodeFlow sc_Flow = flowNodeService.findFlowById(params);
        model.addAttribute("ScFlow", sc_Flow);
        return "zzsc/nodeManager/flowedit_tabpage";
    }

    /**
     * @Title:
     * @Description: TODO 保存流程
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/21
     **/
    @NLog
    @AuthOpt(code = "ZZSC_NODEMANAGER_EDITFLOW")
    @RequestMapping(value = "/edit_flow_save")
    @ResponseBody
    public DggRestResponse EditNodeFlowSave(@RequestParam Map params) {
        try {
            flowNodeService.saveFlow(params);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 节点管理——打开流程节点编辑页
     *
     * @return
     */
    @RequestMapping(value = "/open_edit_flownode_page")
    public String OpenEditFlowNodePage(@RequestParam Map params, Model model) {
        params.put("flag", "1");
        ProductNodeFlow sc_Flow = flowNodeService.findFlowById(params);

        Map nodeFieldQueryMap = new HashMap();
        nodeFieldQueryMap.put("bigformatCode", sc_Flow.getBigformatCode());
        nodeFieldQueryMap.put("flag", "1");
        List<ProductNodeField> sc_NodeFieldList = flowNodeService.findNodefield(nodeFieldQueryMap);

        Map nodeQueryMap = new HashMap();
        nodeQueryMap.put("flowId", params.get("flowId").toString());
        nodeQueryMap.put("flag", "1");
        List<ProductNode> sc_NodeList = flowNodeService.findNodeCenterById(nodeQueryMap);
        ProductNode nodeJd = flowNodeService.findNodeJdById(nodeQueryMap, sc_NodeList);
        ProductNode nodeQzWj = flowNodeService.findNodeQzWjById(nodeQueryMap, sc_NodeList);
        ProductNode nodeWj = flowNodeService.findNodeWjById(nodeQueryMap, sc_NodeList);

        model.addAttribute("ScFlow", sc_Flow);
        model.addAttribute("NodeFieldList", sc_NodeFieldList);
        model.addAttribute("NodeJd", nodeJd);
        model.addAttribute("NodeQzWj", nodeQzWj);
        model.addAttribute("NodeWj", nodeWj);
        model.addAttribute("NodeList", sc_NodeList);
        return "zzsc/nodeManager/flownode_edit_page";
    }

    /**
     * @Title:
     * @Description: TODO 节点保存
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/21
     **/
    @NLog
    @AuthOpt(code = "ZZSC_NODEMANAGER_EDITFLOWNODE")
    @RequestMapping(value = "/nodesave")
    @ResponseBody
    public DggRestResponse NodeSave(ProductNode node, @RequestParam Map params) {
        try {
            flowNodeService.saveNode(node, params);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 节点管理——复制流程页
     *
     * @return
     */
    @RequestMapping(value = "/flowcopy_tabpage")
    public String FlowCopy(@RequestParam Map params, Model model) {
        params.put("flag", "1");
        ProductNodeFlow sc_Flow = flowNodeService.findFlowById(params);
        model.addAttribute("ScFlow", sc_Flow);
        return "zzsc/nodeManager/flowcopy_tabpage";
    }


    /**
     * @Title:
     * @Description: TODO 复制流程节点保存
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/25
     **/
    @AuthOpt(code = "ZZSC_NODEMANAGER_COPYFLOW")
    @RequestMapping(value = "/copy_flow_save")
    @ResponseBody
    public DggRestResponse CopyNodeFlowSave(@RequestParam Map params) {
        try {
            flowNodeService.copyFlowAndFlow(params);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

	
	
}